<?php
/**
 * User: b179a9
 * Date: 10/13/11
 * Time: 6:18 PM
 */

class QuestsController extends Zend_Controller_Action
{

    /**
     * FlashMessenger
     *
     * @var Lib_Flash
     */
    protected $_flashMessenger = null;

    public function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
        } else {
            $this->_helper->layout()->setLayout('admin-layout');
        }
        $this->_flashMessenger = new Lib_Flash();
    }

    public function __call($name, $args)
    {
        $this->_forward('index');
    }

    public function indexAction()
    {

        $manager = new \Managers\Map();
        $maps = $manager->getList();

        $this->view->maps = $maps;
    }

    public function editAction()
    {
        $id = $this->_request->getParam('id');
        $mapsManager = new \Managers\Map();
        $map = $mapsManager->getById($id);

        if (!$map) {
            $this->_flashMessenger->addMessage('Quest does not exist.', Lib_Flash::ERROR, Lib_Flash::NOW);
            $this->_redirect('/quests');
        }
        $form = new App_Form_Quest_Form();
        $form->populate($map);

        if ($this->_request->isPost()) {
            if ($form->isValid($this->_request->getParams())) {
                $values = $form->getValues();
                try {
                    $mapsManager->save($values);
                    $this->_flashMessenger->addMessage('Quest successfully updated.', Lib_Flash::NOTICE, Lib_Flash::REDIRECT);
                    $this->_redirect('/quests');
                } catch (ImagickException $e) {
                    $this->_flashMessenger->addMessage('This file is unsupported.', Lib_Flash::ERROR, Lib_Flash::NOW);
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                }
            } else {
                $this->_flashMessenger->addMessage('Please, check input data.', Lib_Flash::ERROR, Lib_Flash::NOW);
            }

        }
        $this->view->map = $map;
        $this->view->form = $form;
    }

    public function addAction()
    {
        $form = new App_Form_Quest_Form();
        if ($this->_request->isPost()) {
            if ($form->isValid($this->_request->getParams())) {
                $values = $form->getValues();
                $mapsManager = new \Managers\Map();
                try {
                    $mapsManager->save($values);
                    $this->_flashMessenger->addMessage('Quest successfully created.', Lib_Flash::NOTICE, Lib_Flash::REDIRECT);
                    $this->_redirect('/quests');
                } catch (ImagickException $e) {
                    $this->_flashMessenger->addMessage('This file is unsupported.', Lib_Flash::ERROR, Lib_Flash::NOW);
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                }

            } else {
                $this->_flashMessenger->addMessage('Please, check input data.', Lib_Flash::ERROR, Lib_Flash::NOW);
            }
        }

        $this->view->form = $form;
    }

    public function deleteAction()
    {
        $id = $this->_request->getParam('id');

        $mapsManager = new \Managers\Map();
        try {
            $result = $mapsManager->delete($id);
            if ($result) {
                $this->_flashMessenger->addMessage('Quest successfully deleted.', Lib_Flash::NOTICE, Lib_Flash::REDIRECT);
            } else {
                $this->_flashMessenger->addMessage('Quest does not exist.', Lib_Flash::ERROR, Lib_Flash::REDIRECT);
            }
        } catch (Exception $e) {
            $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::REDIRECT);
        }
        $this->_redirect('/quests');
    }

    /**
     * Задает победителя соревнований.
     * @return void
     */
    public function winnerAction()
    {
        $id = $this->_request->getParam('id');
        $type = $this->_request->getParam('type', 'set');
        $manager = new \Managers\Answers();
        try {
            $winnerAttribute = 'set' == $type;
            $answer = $manager->setWinner($id, $winnerAttribute);

            $this->_flashMessenger->addMessage('Winner successfully updated.', Lib_Flash::NOTICE, Lib_Flash::REDIRECT);

            //Если ответ не правильный и задаем победителя, выводим предупреждение.
            if (!$answer->getIsCorrect() && $winnerAttribute) {
                $this->_flashMessenger->addMessage('Answer is not correct. Please, check.', Lib_Flash::WARNING, Lib_Flash::REDIRECT);
            }

        } catch (Exception $e) {
            $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::REDIRECT);
        }

        $this->_redirect('/quests');
    }

    /**
     * Просмотр ответов к карте.
     * @return void
     */
    public function answersAction()
    {
        $id = $this->_request->getParam('id');

        $answersManager = new \Managers\Answers();
        $filter = array('map' => $id);
        $answers = $answersManager->getList($filter);

        $this->view->answers = $answers;

    }

}
